package com.gk.panda.apis.patrol.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.patrol.commons.GeoUtils;
import com.gk.panda.apis.patrol.servicr.LocationInfoService;
import com.gk.panda.apis.patrol.servicr.PatrolService;
import com.gk.panda.apis.patrol.servicr.ReportService;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.patrol.dto.DetailListDTO;
import com.gk.panda.pojo.patrol.dto.PatrolPageDTO;
import com.gk.panda.pojo.patrol.dto.PatrolSumDTO;
import com.gk.panda.pojo.patrol.dto.UpdateInfoDTO;
import com.gk.panda.pojo.patrol.entity.LocationInfoEntity;
import com.gk.panda.pojo.patrol.request.PatrolQueryPageRequest;
import com.gk.panda.pojo.patrol.request.UpdatePatrolRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author：xxz
 * @Create: 2022/11/1 14:36
 */
@RestController
@RequestMapping("/patrol")
@Api(tags = "巡护管理台相关接口")
public class PatrolController {

    @Autowired
    private PatrolService patrolService;

    @Resource
    private ReportService reportService;

    @GetMapping("/count")
    @ApiOperation("巡护统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "totalOrgId",value = "所属机构"),
            @ApiImplicitParam(name = "branchOrgId",value = "保护站"),
            @ApiImplicitParam(name = "name",value = "巡护人员姓名"),
            @ApiImplicitParam(name = "type",value = "巡护类型,TRANSECT:样线巡护，DAILY:日常巡护"),
            @ApiImplicitParam(name = "state",value = "巡护状态，UNDERWAY:巡护中，END:巡护结束"),
            @ApiImplicitParam(name = "startTime",value = "巡护时间筛选开始格式yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "endTime",value = "巡护时间筛选结束格式yyyy-MM-dd HH:mm:ss")
    })
    private Result<PatrolSumDTO> count(PatrolQueryPageRequest request){
        return Result.ok(patrolService.countData(request));
    }

    @GetMapping("/page")
    @ApiOperation("分页查询巡护")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "totalOrgId",value = "所属机构"),
            @ApiImplicitParam(name = "branchOrgId",value = "保护站"),
            @ApiImplicitParam(name = "name",value = "巡护人员姓名"),
            @ApiImplicitParam(name = "type",value = "巡护类型,TRANSECT:样线巡护，DAILY:日常巡护"),
            @ApiImplicitParam(name = "state",value = "巡护状态，UNDERWAY:巡护中，END:巡护结束"),
            @ApiImplicitParam(name = "startTime",value = "巡护时间筛选开始格式yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "endTime",value = "巡护时间筛选结束格式yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "pageNum",value = "分页页数"),
            @ApiImplicitParam(name = "pageSize",value = "分页条数")
    })
    private Result<Page<PatrolPageDTO>> page(PatrolQueryPageRequest request){
        return Result.ok(patrolService.page(request));
    }






    @DeleteMapping("/delete")
    @ApiOperation("删除巡护")
    private Result<?> remove(@NotNull @RequestParam Long id){
        return patrolService.remove(id);
    }

    @PutMapping("/update")
    @ApiOperation("修改巡护")
    private Result<?> update(@Validated UpdatePatrolRequest request){
        return patrolService.update(request);
    }

    @ApiOperation("导出巡护信息")
    @GetMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "totalOrgId",value = "所属机构"),
            @ApiImplicitParam(name = "branchOrgId",value = "保护站"),
            @ApiImplicitParam(name = "name",value = "巡护人员姓名"),
            @ApiImplicitParam(name = "type",value = "巡护类型,TRANSECT:样线巡护，DAILY:日常巡护"),
            @ApiImplicitParam(name = "state",value = "巡护状态，UNDERWAY:巡护中，END:巡护结束"),
            @ApiImplicitParam(name = "startTime",value = "巡护时间筛选开始格式yyyy-MM-dd HH:mm:ss"),
            @ApiImplicitParam(name = "endTime",value = "巡护时间筛选结束格式yyyy-MM-dd HH:mm:ss")
    })
    private void export(HttpServletResponse response, PatrolQueryPageRequest request){
        try {
            patrolService.export(response,request);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("导出巡护信息失败");
        }
    }
    @GetMapping("/detail")
    @ApiOperation("查询详情")
    public Result<DetailListDTO> detail(@RequestParam("id") Long id) {
        return Result.ok(reportService.detailList(id));
    }


    @GetMapping("/updateInfo")
    @ApiOperation("获取编辑详情")
    public Result<UpdateInfoDTO> updateInfo(@RequestParam("patrolId") Long patrolId){
        return Result.ok(patrolService.updateInfo(patrolId));
    }


}
